package com.kinyx.framework.image;

import com.kinyx.framework.annotation.assist.CanNotNull;

class ImageSize {

	/** 原图宽度 */
	final int srcWidth;
	/** 原图高度 */
	final int srcHeight;
	/** 原图宽高比 */
	final double srcRatio;

	/** 目标图宽度 */
	final int desWidth;
	/** 目标图高度 */
	final int desHeight;
	/** 目标图宽高比 */
	final double desRatio;

	/** 缩放比 */
	double zoomRatio;

	/** 结果图宽度 */
	int resultWidth;
	/** 结果图高度 */
	int resultHeight;

	/** 差值宽度 */
	int diffWidth;
	/** 差值高度 */
	int diffHeight;

	/**
	 * 构造方法
	 * @param srcWidth 原图宽度
	 * @param srcHeight 原图高度
	 * @param desWidth 目标图宽度
	 * @param desHeight 目标图高度
	 */
	ImageSize(@CanNotNull final int srcWidth, @CanNotNull final int srcHeight, @CanNotNull final int desWidth, @CanNotNull final int desHeight) {
		this.srcWidth = srcWidth;
		this.srcHeight = srcHeight;
		this.srcRatio = (1.0 * srcWidth) / srcHeight;// 原图宽高比

		this.desWidth = desWidth;
		this.desHeight = desHeight;
		this.desRatio = (1.0 * desWidth) / desHeight;// 目标图宽高比

		final double widthRatio = (1.0 * srcWidth) / desWidth;// 宽度比
		final double heightRatio = (1.0 * srcHeight) / desHeight;// 高度比
		if (widthRatio > heightRatio) {
			// 按照高度缩放
			this.resultWidth = (srcWidth * desHeight) / srcHeight;
			this.resultHeight = desHeight;
			this.zoomRatio = 1 / heightRatio;// 缩放比
			this.diffWidth = this.resultWidth - desWidth;// 差值宽度
		} else if (widthRatio == heightRatio) {
			// 无需缩放
			this.resultWidth = desWidth;
			this.resultHeight = desHeight;
		} else {
			// 按照宽度缩放
			this.resultWidth = desWidth;
			this.resultHeight = (srcHeight * desWidth) / srcWidth;
			this.zoomRatio = 1 / widthRatio;// 缩放比
			this.diffHeight = this.resultHeight - desHeight;// 差值高度
		}
	}

}
